/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "motor_simulation.h"
#include "bsp_dwt.h"
#include "pid.h"
#include "stdio.h"
#include "math.h"
#include "SerialDebug.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
motorObject_t Motor;
motorObject_t Motort;
uint32_t DWT_CNT;
float dt;
PID_t PID;
PID_tt PIDT;
PID_ttt PIDTT;
float Vel,Ang,Angt;
float pi = 3.1415926;
float   t=0;
float refang;
float voltage;
float voltaget;
float weizhipid;
float refVel;
float Velt;
float input1;
float input2;
uint8_t receive_data[500];
int len;
float kp1; 
float kp2; 
float ki1; 
float ki2; 
float kd1; 
float kd2; 
float kd3; 
float kp3; 
float ki3; 
int i ;
float aa;
float  bb;
float cc;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  Motor_Object_Init(&Motor);
	Motor_Object_Init(&Motort);
	PID_Init(&PID,30,0.06,0.06);
	PID_Initt(&PIDT,300,0.5,15);
   PID_Inittt(&PIDTT,0,0,0);
	DWT_Init(72);
	double  a = HAL_GetTick ();
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	HAL_UART_Receive_DMA(&huart1, receive_data, 100);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		
//		double b = HAL_GetTick ();
//		t = (b-a)/1000;
//		dt = DWT_GetDeltaT(&DWT_CNT);
//		Vel = Get_Motor_Velocity(&Motor);
//		Velt = Get_Motor_Velocity(&Motort);
//		Ang = Get_Motor_Angle(&Motor);
//		Angt = Get_Motor_Angle(&Motort);
//	//	float refang = 0;
//	//	float refang = 2*pi*sin(6.5*t);
//		float refang = 2*pi;
//		voltage = PID_Calculatet(&PIDT,refang,Ang,dt);
////    Motor_Simulation(&Motor,voltage+10,dt);
//	printf("a=%f,",Ang   );
////		printf("a=%f,",voltage   );
//		printf("b=%f,",refang  );
////		printf("c=%f,",10.0 );
////		voltaget = PID_Calculatett(&PIDTT,voltage,Angt,dt);
////	Motor_Simulation(&Motort,voltaget,dt);
//	printf("e=%f,",Angt  );


//	
//		weizhipid = PID_Calculatett(&PIDTT,refang,Angt,dt);
//		voltaget = PID_Calculate(&PID,weizhipid,Velt,dt);
//		input1=voltage;
//		input2=voltaget;
//		if(HAL_GPIO_ReadPin(GPIOA ,GPIO_PIN_0)==1)
//		{
//		 input1=10;
//			input2 =10;
//		
//		}
//		Motor_Simulation(&Motor,input1,dt);
//		Motor_Simulation(&Motort,input2,dt);
////			printf("c=%f,",voltaget   );
////		printf("c=%f,",-weizhipid  );
////		printf("e=%f,",Velt   );
////		printf("c=%f,",input2  );
//////////		Serial_Debug(&huart1,refang ,weizhipid ,Vel, 0, 0, 0, 0);
//		
//		
//		
//		
//		
//	HAL_Delay(1);

		double  b = HAL_GetTick ();
		t = (b-a)/1000;
		dt = DWT_GetDeltaT(&DWT_CNT);
		Vel = Get_Motor_Velocity(&Motor);
		Ang = Get_Motor_Angle(&Motor);
		refVel=10;
//		refVel=10*sin(41*t);
		float voltage = PID_Calculate(&PID,refVel,Vel,dt);
		Motor_Simulation(&Motor,voltage,dt);
//		printf("a=%f,",Vel  );
//		printf("b=%f,",refVel  );
//	printf("c=%f,",0.0 );
//		printf("e=%f,",Ang   );
	HAL_Delay(1);
	
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{
 if(__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE)) //????????????
 {	
  __HAL_UART_CLEAR_IDLEFLAG(huart);//??IDLE???
 (void)USART1->SR; //??SR???
 (void)USART1->DR;  //??DR???
  __HAL_DMA_CLEAR_FLAG(huart,DMA_FLAG_TC5);//??DMA???????
	HAL_UART_DMAStop(huart);
	 //???????
  int len=100-__HAL_DMA_GET_COUNTER(huart->hdmarx);
  for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='p'&&receive_data[i+2]=='2')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			kp2=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='i'&&receive_data[i+2]=='1')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			
			ki1=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='d'&&receive_data[i+2]=='1')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
		
			kd1 = g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
		int l = 0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='p'&&receive_data[i+2]=='1')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
				l=c;
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
				
			}
				for(;l+1<b;l++ )
			{
			
			 g = g + (receive_data[c+1]-48)*r;
				c++;
				
				
			 r=r/10;
				
			}
			
			kp1=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			l=0;
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='d'&&receive_data[i+2]=='2')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			printf("%f,",g  );
			kd2=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='i'&&receive_data[i+2]=='1')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			ki1=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='i'&&receive_data[i+2]=='3')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			ki3=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='d'&&receive_data[i+2]=='3')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			kd3=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
	for(i=0;i<len;i++ )
	{
		  float  g=0;
			int c=0;
			int j=0;
			int d=1;
			float  r=0.1;
			int b=0;
			 
		if(receive_data[i]=='k'&&receive_data[i+1]=='p'&&receive_data[i+2]=='3')
		{
			for(b=i;b<len;b++ )
			{
			 if(receive_data[b]==',') 
			break ;
			}
			for(c=i;c<len;c++ )
			{
			 if(receive_data[c]=='.') 
			break ;
			}
			
			for(int n = c-i-4;n>1;n-- )
			{//zhui gao wei 
		   
				d=d*10;
			
			}
				for(j=i+4;j<c;j++ )
			{
				//de dao zheng shu bu fen
			 g = g + (receive_data[j]-48)*d;
			 d=d/10;
			}
				for(;c+1<b;c++ )
			{
				
			 g = g + (receive_data[c+1]-48)*r;
			 r=r/10;
			}
			
			kp3=g;
			g=0;
			c=0;
			j=0;
			d=1;
			r=0.1;
			b=0;
			c=0;
			
		}		
	}
		PID_Init(&PID,kp1,ki1,kd1);
	PID_Initt(&PIDT,kp2,ki2,kd2);
   PID_Inittt(&PIDTT,kp3,ki3,kd3);
  HAL_UART_Receive_DMA(huart,receive_data,100);//??????,NDTR??  
 }
 }
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
